/*
Copyright (C) 2008 Village the Game LLC.

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

var gameRequest = null;
var DETAILS_HTML = "";
var htmlDetailsView = new DetailsView();
var URL = "http://www.villagethegame.com/kenyagdd.html";

function reloadGame(){
	statusLabel.innerText = "Reloading Game";
	//pluginHelper.closeDetailsView();
	htmlDetailsView = new DetailsView();
	
	onTextClick();
}

function getGame(){
	statusLabel.innerText = "Getting Game";
	gameRequest = new XMLHttpRequest();
	try{
		gameRequest.open("GET", URL,true);
	}catch(e){
		statusLabel.innerText = FAILED;
		gameRequest = null;
	return;
	}
	gameRequest.onreadystatechange = onGameData;
	try{
		gameRequest.send(); 
	}catch(e){
		statusLabel.innerText = "Send Failed";
		gameRequest = null;
		return;
	}
}

function onGameData(){
	statusLabel.innerText = "Game Coming";

	if(gameRequest.readyState != 4){
		return;	
	}
	if(gameRequest.status != 200){
		statusLabel.innerText = FAILED;
		gameRequest = null;
		return;
	}
  // Create the details view and set its content. Specifying 'undefined' in 
  // setContent makes it such that no time is displayed in the details view
 
	DETAILS_HTML = gameRequest.responseText;
	htmlDetailsView.setContent("", undefined, DETAILS_HTML, false, 0);
	pluginHelper.showDetailsView(htmlDetailsView, DETAILS_VIEW_TITLE, 
		gddDetailsViewFlagToolbarOpen, onDetailsViewFeedback);

}
function onTextClick() {
	DETAILS_HTML = "<html><body>Waiting for Game</body></html>";
	htmlDetailsView.html_content = true;
	htmlDetailsView.setContent("", undefined, DETAILS_HTML, false, 0);
	
	// In order to have two way communication with the HTML details view, we 
	// need to set an external object that can receive events. This is called 
	// from the HTML using the javascript "window.external.onItemClick(text);".
	var externalObject = new Object();
	externalObject.onItemClick = onItemClick;
	externalObject.getFriends = getFriends;
	externalObject.getTalk = getTalk;
	externalObject.parentHandle = onTalkDataArrival;
	htmlDetailsView.external = externalObject;
	// Show the details view
	htmlDetailsView.setContent("", undefined, DETAILS_HTML, false, 0);	
	getGame();
}

function onItemClick(text) {
  // Display which item was clicked on
  view.alert(text);

  // Close the details view
  //pluginHelper.CloseDetailsView();
}

function getFriends(){
 return googleTalk.friends.toArray();
}

function getTalk(){
 return googleTalk;	
}

function onDetailsViewFeedback(detailsViewFlags) {
  if (detailsViewFlags == gddDetailsViewFlagNone) {
    // User closed the details view
  } else if (detailsViewFlags == gddDetailsViewFlagToolbarOpen) {
    // User clicked on the title of the details view
  }
}


//Google Talk features
// Interval for updating the friends list
var UPDATE_FRIENDS_INTERVAL = 10000;  // 10 seconds

function onOpen() {
  // Set up Google Talk data handler
 // googleTalk.onReceiveTalkData = onTalkDataArrival;

  // Update friends list for the first time
  updateFriends();
  
  // Continue updating the friends list every 10 seconds
  setInterval(updateFriends, UPDATE_FRIENDS_INTERVAL);
}

// Update the list of friends in the UI
function updateFriends() {  
  // Delete all friends from the element list in the UI. container is a div
  // element that was created in the xml.
  var count = container.children.count;
  while (count--) {
    var child = container.children(count);
    container.removeElement(child);
  }

  // Get an array of friends
  var friends = googleTalk.friends.toArray();

  // Loop through all friends and dynamically create a seperate element 
  // for each one.
  for (var i = 0; i <= friends.length - 1; i++) {
    var friend = friends[i];

    var element = container.appendElement("<a x=\"0\" " +
      "y=\"" + (i * 15) + "\" " +
      "color=\"#000080\" font=\"Tahoma\" size=\"8\" " +
      "onClick=\"onClick('" + friend.user_id + "');\" " +
      "onDblClick=\"onDblClick('" + friend.user_id + "');\" />");
    element.tooltip = SEND_MESSAGE_PREFIX + friend.user_id;

    // The status property of the friend object is an integer. Concatenating
    // it to a set prefix results in a string such as "TALK_STATUS_1". Then,
    // this string is used to look up an internationalized string in the
    // strings.xml file (represented by the strings global object).
    element.innerText = friend.name + " (" + strings["TALK_STATUS_" + 
      friend.status] + ")";
  }
}

// Handler for incoming Google Talk data
function onTalkDataArrival(talkFriend, data) {
  reload.innerText = "data " + talkFriend.name;
}

// Handler for clicks on the user links
function onClick(talkUserID) {
  // This sends arbitrary data to the counterpart gadget on a friend's sidebar.
  // Note that if the sidebar is closed, or the gadget isn't active, the user
  // is prompted to open the sidebar or install the gadget. In most cases,
  // such as sending a news item, this is desirable. If, however, you wish
  // message delivery to fail silently, you can use the following syntax:
  //  googleTalk.SendTalkDataEx(talk_user_id, 'Data here...',
  //    gddSendDataFlagSilent);
  googleTalk.SendTalkData(talkUserID, DATA);
}

// Handler for double-clicks on the user links
function onDblClick(talkUserID) {
  // This opens a Google Talk conversation window with the specified friend
  googleTalk.SendTalkText(talkUserID, SENT_FROM_GADGET);
}

